fix: delete instance cleanup on baileys failure#2508
Conversation
There was a problem hiding this comment.
Sorry @antonio-abrantes, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
Hi! As part of our PR triage, we re-ran CI on this PR and the Check Code Quality workflow is failing. Could you please:
Once CI is green I'll re-review for merge. Thanks! |
|
Boa refatoração — quebrar o try/catch monolítico em steps isolados é a forma idiomática correta de cleanup robusto, e adicionar Bloqueadores para o merge:
Após os 3 itens, aprovo. |
|
Olá @antonio-abrantes, obrigado pela contribuição — o fix está correto e endereça um problema real de produção (instâncias travadas no A PR está em estado git fetch upstream
git rebase upstream/develop
git push --force-with-leaseApós o rebase, mergeio. Obrigado! |
📋 Description
🐛 Problem
When calling
DELETE /instance/delete/{instanceName}on a corruptedinstance (disconnected/bugged Baileys session), the API returns:
{ "status": 400, "error": "Bad Request", "response": { "message": ["[object Object]"] } }After this error, the instance name remains permanently blocked in
waInstancesmemory. Even after manually cleaning the database, Redis,and session files, calling
POST /instance/createwith the same namereturns:
{ "status": 403, "message": ["This name \"agent-xxx\" is already in use."] }The only workaround was restarting the container — not viable in production environments.
🔍 Root Cause
Two bugs working together:
1. Aborted cleanup
deleteInstance()stopped execution iflogout()failed2. Bad error serialization
[object Object]✅ Fix
Split
try/catchinto isolated stepsEnsure cleanup always runs
Always remove instance from
waInstancesFix error serialization using:
🔗 Related Issue
Closes # (leave empty if not applicable)
🧪 Type of Change
🧪 Testing
📸 Screenshots (if applicable)
N/A
✅ Checklist
📝 Additional Notes
Tested in production environment with Docker Swarm + PostgreSQL + Redis.
Fix only affects failure path — no impact on healthy instances.